﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http.Filters;
using System.Net.Http;

namespace ERPStore.Web.Controllers.ActionFilters
{
	public class ValidationFilterAttribute : ActionFilterAttribute
	{
		public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
		{
			if (actionExecutedContext.Exception != null)
			{
				var httpError = new System.Web.Http.HttpError(actionExecutedContext.Exception, true);
				var errorCode = System.Net.HttpStatusCode.InternalServerError;
				if (actionExecutedContext.Exception is ERPStore.Models.ValidationException)
				{
					var ex = actionExecutedContext.Exception as ERPStore.Models.ValidationException;
					httpError.Add("BrokenRuleList", ex.List);
					errorCode = System.Net.HttpStatusCode.Gone;
				}
				actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(errorCode, httpError);
			}
			base.OnActionExecuted(actionExecutedContext);
		}
	}
}
